package com.jgsu.farm.controller;

import com.jgsu.farm.entity.Analysis;
import com.jgsu.farm.entity.Question;
import com.jgsu.farm.exception.*;
import com.jgsu.farm.service.AnalysisService;
import com.jgsu.farm.utils.CommonResult;
import com.jgsu.farm.utils.ResultCode;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;


@Log4j2
@Api(value = "farm/sysuser/analysis", tags = "情报分析管理模块")  //描述信息用的，标注在类上
@RestController
@RequestMapping("farm/sysuser/analysis")
public class AnalysisController {
    @Autowired
    AnalysisService analysisService;

    /**
     * 情报分析增加
     * */
    @ApiOperation(value = "情报分析添加功能")
    @PostMapping(value = "/add", produces = "application/json")
    public CommonResult AnalysisAdd(Analysis analysis) {
        try {
            analysisService.add(analysis);
           log.info("情报分析数据添加，id为{}",analysis.getInfoid());
            return CommonResult.success(analysis);
        } catch (DataNotExistException e) {
            log.error("添加id = {}的情报分析数据失败",analysis.getInfoid());
            throw new DataNotExistException(e.getCode(), e.getMessage());
        }
    }

    /**
     * 情报分析删除
     * */
    @ApiOperation(value = "情报分析删除功能")
    @DeleteMapping(value = "/delete", produces = "application/json")
    public CommonResult AnalysisDelete(@RequestParam("analysisid") Integer analysisid) {
        try {
            analysisService.delete(analysisid);
            log.info("执行删除id = {}的情报分析数据",analysisid);
            return CommonResult.success(ResultCode.SUCCESS);
        } catch (DataNotExistException e) {
            log.error("id = {}的情报分析数据删除失败",analysisid);
            throw new DataNotExistException(e.getCode(), e.getMessage());
        }
    }

    /**
     * 情报分析查询
     */
    @ApiOperation(value = "情报分析查询功能")
    @GetMapping(produces = "application/json")
    public CommonResult<PageInfo<Analysis>> AnalysisFind(@RequestParam(value = "page", required = false, defaultValue = "1") int page,
                                                         @RequestParam(value = "pagesize", required = false, defaultValue = "10") int pagesize) {
        try {
            PageInfo<Analysis> analysises = analysisService.selectAll(page, pagesize);
            log.info("查询所有的咨询信息");
            return CommonResult.success(analysises);
        } catch (DataNotExistException e) {
            log.error("咨询信息表中没有数据");
            throw new DataNotExistException(e.getCode(), e.getMessage());
        }
    }

    /**
     * 情报分析修改
     * */
    @ApiOperation(value = "情报分析修改功能")
    @PutMapping(value = "/update", produces = "application/json")
    public CommonResult AnalysisModify(Analysis analysis) {
        try {
            analysisService.update(analysis);
            log.info("更新{}的情报分析内容",analysis.getAnalysis());
            return CommonResult.success(analysis);
        } catch (DataNotExistException e) {
            log.error("{}的情报分析内容更新失败",analysis.getAnalysis());
            throw new DataNotExistException(e.getCode(), e.getMessage());
        }
    }
}
